﻿using IRepositoryBll;
using IRepositoryDal;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositorySystem.Models;
using RepositorySystem.Models.WorkFlow_ModelViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositorySystem.Controllers
{
    public class WorkFlow_ModelController : Controller
    {
        private IWorkFlow_ModelBll _workFlow_ModelBll;
        private IWorkFlow_ModelDal _workFlow_ModelDal;
        public WorkFlow_ModelController(IWorkFlow_ModelBll workFlow_ModelBll, IWorkFlow_ModelDal workFlow_ModelDal)
        {
            _workFlow_ModelBll = workFlow_ModelBll;
            _workFlow_ModelDal = workFlow_ModelDal;
        }

        /// <summary>
        /// 工作流模板表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult WorkFlow_ModelListView()
        {
            return View();
        }

        /// <summary>
        /// 添加工作流模板表页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddWorkFlow_ModelView(AddWorkFlow_ModelViewModels model)
        {
            return View();
        }

        /// <summary>
        /// 添加工作流模板表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddWorkFlow_Model(AddWorkFlow_ModelViewModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (model.Title == null)
            {
                res.Msg = "模板标题不能为空";
                return Json(res);
            }

            WorkFlow_Model workFlow_Model = new WorkFlow_Model()
            {
                ID = Guid.NewGuid().ToString(),
                Title = model.Title,
                Description = model.Description,
                IsDelete = false,
                CreateTime = DateTime.Now


            };
            bool isSuccess = _workFlow_ModelBll.Add(workFlow_Model);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                return Json(res);
            }
            else
            {
                return Json(res);
            }

        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public IActionResult GetWorkFlow_ModelList(int page, int limit, string title)
        {
            int count;
            object workFlow_Model = _workFlow_ModelBll.GetWorkFlow_ModelListByPage(page, limit, out count, title);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = workFlow_Model
            });
        }

        /// <summary>
        /// 工作流模板表修改页面
        /// </summary>
        /// <param name="workFlow_ModelId"></param>
        /// <returns></returns>
        public IActionResult UpdateWorkFlow_ModelView(string workFlow_ModelId)
        {
            return View();
        }

        /// <summary>
        /// 获取修改工作模板信息
        /// </summary>
        /// <param name="workFlow_ModelId"></param>
        /// <returns></returns>
        public IActionResult GetUpdateWorkFlow_Model(string workFlow_ModelId)
        {
            CustomActionResult res = new CustomActionResult();
            WorkFlow_Model workFlow_Model = _workFlow_ModelBll.GetEntity(workFlow_ModelId);
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                WorkFlow_Model = workFlow_Model
            };

            return Json(res);
        }


        /// <summary>
        /// 更新修改工作模板表信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateWorkFlow_Model(UpdateWorkFlow_ModelViewModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_workFlow_ModelBll.UpdateWorkFlow_Model(model.ID, model.Title, model.Description))
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "成功";

            }
            return Json(res);
        }

        public IActionResult DeleteWorkFlow_Model(string workFlow_ModelId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_workFlow_ModelBll.Delete(workFlow_ModelId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }
    }
}
